#include "ResultModel.h"

ResultModel::ResultModel(QObject *parent)
    : QAbstractTableModel(parent)
{

}


QVariant ResultModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();

    if(role==Qt::DisplayRole){
        return hash[index.row()].value(index.column());
    }
    return QVariant();
}


QVariant ResultModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
    {
        if (orientation == Qt::Horizontal)
        {
            return m_header[section];
        }
    }

    return QVariant();
}

void ResultModel::setTableValue(QHash<int, QVariantList> data)
{
    hash = data;
}

void ResultModel::insertRowData(int index,QVariantList data)
{
    beginInsertRows(QModelIndex(), hash.size(), hash.size());
    hash.insert(index,data);
    endInsertRows();
}

void ResultModel::appendRowData(QVariantList data)
{
    if(hash.isEmpty()){
        insertRowData(0,data);
    }else {
        insertRowData(hash.size(),data);
    }

}

void ResultModel::modifyData(int row,int column,QVariant data)
{
    if (row == -1 || column == -1){return;}
    beginResetModel();
    hash[row].replace(column,data);
    endResetModel();
}

void ResultModel::refresh()
{
    beginResetModel();
    endResetModel();
}

void ResultModel::replaceModel(QHash<int, QVariantList> value)
{
    beginResetModel();
    hash.clear();
    hash = value;
    endResetModel();
}



bool ResultModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
    if (value != headerData(section, orientation, role)) {
        // FIXME: Implement me!
        emit headerDataChanged(orientation, section, section);
        return true;
    }
    return false;
}


int ResultModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return this->rowCount();
}

int ResultModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return this->columnCount();
}



bool ResultModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (data(index, role) != value) {
        // FIXME: Implement me!
        emit dataChanged(index, index, QVector<int>() << role);
        return true;
    }
    return false;
}

Qt::ItemFlags ResultModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;

    return Qt::ItemIsEditable; // FIXME: Implement me!
}

bool ResultModel::insertRows(int row, int count, const QModelIndex &parent)
{
    beginInsertRows(parent, row, row + count - 1);
    // FIXME: Implement me!
    endInsertRows();
    return true;
}

bool ResultModel::insertColumns(int column, int count, const QModelIndex &parent)
{
    beginInsertColumns(parent, column, column + count - 1);
    // FIXME: Implement me!
    endInsertColumns();
    return true;
}

bool ResultModel::removeRows(int row, int count, const QModelIndex &parent)
{
    beginRemoveRows(parent, row, row + count - 1);
    // FIXME: Implement me!
    endRemoveRows();
    return true;
}

bool ResultModel::removeColumns(int column, int count, const QModelIndex &parent)
{
    beginRemoveColumns(parent, column, column + count - 1);
    // FIXME: Implement me!
    endRemoveColumns();
    return true;
}
